package assignmentstrategy

import (
	"codejy/analysis/codeanalysis"
	"codejy/globalfunc"
	"codejy/store"
	"codejy/structdef"
	"strings"
)

type InstanceAddressMethodStrategy struct {
}

func init() {
	RegisterStrategy(&InstanceAddressMethodStrategy{})
}
func (n InstanceAddressMethodStrategy) getId() int {
	return codeanalysis.InstanceAddressMethod
}

func (n InstanceAddressMethodStrategy) Execute(threadStore *structdef.ThreadStore, code string) string {
	return store.NweStatusRestore(threadStore, func() string {
		//通过 实例地址.行为名称() ,获取值
		//截取实例地址,,获取最后@#@位置
		index := strings.LastIndex(code, "@#@.")
		instanceAddress := code[:index+3]
		threadStore.CurrentInstanceAddress = instanceAddress
		instance, _ := store.GetInstanceProp(threadStore, instanceAddress)
		typeName, _ := instance["@TYPENAME@"]  //获取实例的类型名称
		scope, _ := instance["@TYPEFILEPATH@"] //获取实例的类型名称
		interfaces := globalfunc.ExecuteFunc("SubFunctionCallNameAndParam", code[index+4:]).([]interface{})
		funName, funParams := interfaces[0].(string), interfaces[1].([]string)

		typeDefinition := store.GetTypeDefinition(scope.Value, typeName.Value)
		definition, p := typeDefinition.Methods[funName]
		if p {
			globalfunc.ExecuteFunc("ExecuteTypeMethod", threadStore, funName, definition, funParams) //执行方法
			//获取函数返回值
			variable1, p1 := store.GetVariable(threadStore, funName)
			if !p1 {
				return ""
			}
			return variable1.Value
		} else {
			prop, _ := store.GetInstanceProp(threadStore, instanceAddress)
			typeName, _ = prop["@TYPENAME@"] //获取实例的类型名称
			panic("错误::::::" + typeName.Value + "类型中没有" + code + "行为方法的定义")
		}

	})
}
